﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataLayer;
using OrderDocument;
using System.Threading;

namespace Manager
{
    public partial class frmMain : XtraForm
    {
        int printCount = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["Print"]);

        public frmMain()
        {
            InitializeComponent();
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            xtraTabControl1.SelectedTabPageIndex = 0;
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            gridControl1.DataSource = new dbListe().EvrakListesi();
            xtraTabControl1.SelectedTabPageIndex = 1;
            gridView1.BestFitColumns();
        }

        private void btnDoviz_Click(object sender, EventArgs e)
        {
            Factory.GetFactory.ExecuteNonQuery(CommandType.Text, "DELETE FROM CURRENCY");
            IDbDataParameter prm = Factory.GetFactory.GetParameter("@rate", DbType.Decimal);
            IDbDataParameter prm2 = Factory.GetFactory.GetParameter("@rate", DbType.Decimal);
            prm.Value = txtUsd.Text;
            Factory.GetFactory.ExecuteNonQuery(CommandType.Text, "INSERT INTO CURRENCY (name,rate) VALUES ('USD',@rate)", prm);
            prm2.Value = txtEuro.Text;
            Factory.GetFactory.ExecuteNonQuery(CommandType.Text, "INSERT INTO CURRENCY (name,rate) VALUES ('EURO',@rate)", prm2);
            MessageBox.Show("Kaydedildi !");
        }

        private void navBarItem3_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmImports imp = new frmImports();
            imp.ShowDialog(this);
        }

        private void yazdırToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                foreach (int row in gridView1.GetSelectedRows())
                {
                    string seri = gridView1.GetRowCellDisplayText(row, gridView1.Columns["Evrak Seri"]);
                    string sira = gridView1.GetRowCellDisplayText(row, gridView1.Columns["Evrak Sıra"]);
                    prnSiparis prn = new prnSiparis();
                    prn.EvrakSeri = seri;
                    prn.EvrakSira = int.Parse(sira);
                    prn.EvrakYukle(false);
                    prn.PrintingSystem.ShowMarginsWarning = false;
                    prn.Print();
                }
            }
        }

        private void yazıcıÖnizlemeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                string seri = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["Evrak Seri"]);
                string sira = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["Evrak Sıra"]);
                prnSiparis prn = new prnSiparis();
                prn.EvrakSeri = seri;
                prn.EvrakSira = int.Parse(sira);
                prn.EvrakYukle(true);
                prn.ShowPreview();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            DataTable dt = Factory.GetFactory.ExecuteDataTable("SELECT name,rate FROM CURRENCY");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["name"].ToString())
                {
                    case "USD":
                        txtUsd.Text = dt.Rows[i]["rate"].ToString();
                        break;
                    case "EURO":
                        txtEuro.Text = dt.Rows[i]["rate"].ToString();
                        break;
                    default:
                        break;
                }
            }
        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            xtraTabControl1.SelectedTabPageIndex = 2;
            DataTable dt = Factory.GetFactory.ExecuteDataTable("SELECT     barcode AS Barkodu, name AS [Stok Adı], price AS Fiyatı, vat AS Kdv, supplier AS Sağlayıcısı,(case when unit = '0' then 'Adet' when unit='1' then 'Kutu' else 'Koli' end ) as [Birim] FROM         PRODUCTS WITH (NOLOCK) WHERE     (new = 1)");
            gridControl2.DataSource = dt;
            gridView2.BestFitColumns();
        }

        private void navBarItem5_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmMessage msg = new frmMessage();
            msg.ShowDialog(this);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DbMikro mik = new DbMikro();
            mik.Depo = int.Parse(txtDepono.Text);
            mik.MikroServer = txtSunucu.Text;
            mik.MikroDb = txtVt.Text;
            mik.SormMerkezi = txtSrm.Text;
            mik.Sifre = txtSifre.Text;
            mik.Tedarikci = txtTedarikci.Text;
            mik.EvrakAktar();
        }

        private void navBarItem6_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            xtraTabControl1.SelectedTabPageIndex = 3;
        }

        private void navBarItem7_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmCekilis cek = new frmCekilis();
            cek.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                timer1.Stop();
                timer1.Enabled = false;
                Application.DoEvents();
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable dt = Factory.GetFactory.ExecuteDataTable("exec sp_executesql N'SELECT     order_series, order_no FROM         ORDER_DETAILS WITH (NOLOCK) WHERE     (printed = 0) AND (terminal = 1) GROUP BY order_series, order_no '");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string seri = dt.Rows[i]["order_series"].ToString();
                    string sira = dt.Rows[i]["order_no"].ToString();

                    using (prnSiparis prn = new prnSiparis())
                    {
                        prn.EvrakSeri = seri;
                        prn.EvrakSira = int.Parse(sira);
                        prn.EvrakYukle(false);
                        prn.PrintingSystem.ShowMarginsWarning = false;

                        for (int z = 0; z < printCount; z++)
                        {
                            prn.Print();
                        }
                    }
                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            timer1.Interval = 15000;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void exeleAktarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string yol = dlg.SelectedPath;
                if (gridView1.SelectedRowsCount > 0)
                {
                    foreach (int row in gridView1.GetSelectedRows())
                    {
                        string seri = gridView1.GetRowCellDisplayText(row, gridView1.Columns["Evrak Seri"]);
                        string sira = gridView1.GetRowCellDisplayText(row, gridView1.Columns["Evrak Sıra"]);
                        prnSiparis prn = new prnSiparis();
                        prn.EvrakSeri = seri;
                        prn.EvrakSira = int.Parse(sira);
                        prn.EvrakYukle(false);
                        prn.ExportToPdf(yol + "\\" + seri + "_" + sira + ".pdf");
                    }

                    MessageBox.Show("Tamamlandı !");
                }
            }
        }

        private void exceleAktarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string yol = dlg.SelectedPath;
                if (gridView1.SelectedRowsCount > 0)
                {
                    foreach (int row in gridView1.GetSelectedRows())
                    {
                        string seri = gridView1.GetRowCellDisplayText(row, gridView1.Columns["Evrak Seri"]);
                        string sira = gridView1.GetRowCellDisplayText(row, gridView1.Columns["Evrak Sıra"]);
                        prnSiparis prn = new prnSiparis();
                        prn.EvrakSeri = seri;
                        prn.EvrakSira = int.Parse(sira);
                        prn.EvrakYukle(false);
                        prn.ExportToXls(yol + "\\" + seri + "_" + sira + ".xls");
                    }

                    MessageBox.Show("Tamamlandı !");
                }
            }
        }

        private void navBarItem8_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (navBarItem8.Caption == "Yazdırma Durdur")
            {
                timer1.Stop();
                timer1.Enabled = false;
                navBarItem8.Caption = "Yazdırma Başlat";
            }
            else
            {
                navBarItem8.Caption = "Yazdırma Durdur";
                timer1.Enabled = true;
                timer1.Start();
            }
        }
    }
}
